package youleche.com.controler.servlet;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import freemarker.template.Configuration;

import youleche.com.business.BShare;
import youleche.com.business.BVehicleImageInfo;
import youleche.com.controler.servlet.common.UlecheHttpServlet;
import youleche.com.interfaces.LoginAction;
import youleche.com.model.MVehicleImageInfo;
import youleche.com.util.FileUtil;
import youleche.com.util.JsonHelper;
import youleche.com.util.StaticUtil;

/**
 * Servlet implementation class SShareServletAuthority
 */
public class SShareServletAuthority extends UlecheHttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SShareServletAuthority() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
	 * freemark静态化配置类
	 */
	 private Configuration cfg = null;
	 
	 @Override
     public void init() throws ServletException
     {
        cfg = new Configuration();
        cfg.setServletContextForTemplateLoading(this.getServletContext(), null);
     }
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		int action = Integer.parseInt(request.getParameter("action"));
		switch(action){
			case 0: 
				//获取分享功能列表信息
				this.getList_Au(request, response);
				break;
			case 1: 
				//获取分享功能列表总条数信息（根据用户名）
				this.getCount_Au(request, response);
				break;
			case 2:
				//删除分享信息
				this.deleteShare_Au(request, response);
				break;
				
		}
	}
	
	/**
	 * 删除分享信息
	 * @param request 请求
	 * @param response 响应
	 */
	private void deleteShare_Au(HttpServletRequest request, HttpServletResponse response){
		LoginAction loginHandler = new LoginAction();
		if(!loginHandler.is_Login(request, response)){ //未登录，不能删除信息
			super.response(response, "0");
			return;
		}
		
		String id = request.getParameter("id");
		/*
		 1.删除分享信息
		 2.删除upload里面的文件
		 3.删除图片数据库信息
		 4.静态化骑游首页轮播图片
		 5.静态化骑游首页列表
		 6.删除静态化页面详细页面
		 */
		
		BShare handler = new BShare();
		BVehicleImageInfo imageHandler = new BVehicleImageInfo();
		try {
			//1.删除分享信息
			if(handler.deleteShareById(id) < 1){
				super.response(response, "0");
				return;
			}
			
			//2.删除upload里面的文件
			List<MVehicleImageInfo> imgList = imageHandler.getImageInfoByVehicleId(id);
			if(imgList != null && imgList.size() > 0){
				for (MVehicleImageInfo img : imgList) {
					FileUtil.deleteFile(img.getImagePath(), "upload/");
				}
			}
			//3.删除图片数据库信息
			imageHandler.deleteImageByVehicleId(id);			
			//4.静态化骑游首页轮播图片
			StaticUtil.staticQyPlaying(cfg, request, response);
			//5.静态化骑游首页列表			
			StaticUtil.staticShareList(cfg, request, response);			
			//6.删除静态化页面详细页面
			//System.out.println(request.getSession().getServletContext().getRealPath("") + "/qy/html/" + id + ".html");
			FileUtil.deleteFile(id + ".html", request.getSession().getServletContext().getRealPath("") + "/qy/html/");
			super.response(response, "1");
		} catch (Exception e) {
			e.printStackTrace();
			super.response(response, "0");
		}
		
		
	}
	
	/**
	 * 获取分享功能列表总条数信息（根据用户名）
	 * @param request
	 * @param response
	 */
	private void getCount_Au(HttpServletRequest request, HttpServletResponse response){
		LoginAction loginHandler = new LoginAction();
		if(!loginHandler.is_Login(request, response)){ //未登录，不能获取列表信息
			super.response(response, "");
			return;
		}
		
		String userName = loginHandler.getLoginName(request, response);
		if("".equals(userName)){ //用户名为空，返回空
			super.response(response, "");
			return;
		}
		
		BShare handler = new BShare();
		try {
			super.response(response, ""+handler.getShareCountByUserName(userName));
		} catch (Exception e) {
			e.printStackTrace();
			super.response(response, "");
		}
	}
	
	
	/**
	 * 获取分享功能列表信息
	 * @param request
	 * @param response
	 */
	private void getList_Au(HttpServletRequest request, HttpServletResponse response){
		LoginAction loginHandler = new LoginAction();
		if(!loginHandler.is_Login(request, response)){ //未登录，不能获取列表信息
			super.response(response, "");
			return;
		}
		
		String userName = loginHandler.getLoginName(request, response);
		if("".equals(userName)){ //用户名为空，返回空
			super.response(response, "");
			return;
		}
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		Integer startIndex = (Integer.parseInt(request.getParameter("pageIndex")) - 1) * pageSize ;
		
		BShare handler = new BShare();
		try {
			JsonHelper.printListToJsonString(response, handler.getShareListByUserName(startIndex.toString(), ((Integer)pageSize).toString(), " order by addTime desc ", userName));
		} catch (Exception e) {
			e.printStackTrace();
			super.response(response, "");
		}
	}

}
